package org.jeecg.modules.golf.plan.query.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.golf.plan.client.PlanPage;
import org.jeecg.modules.golf.plan.client.PlanQueryDTO;
import org.jeecg.modules.golf.plan.entity.Plan;
import org.jeecg.modules.golf.plan.entity.PlanTypeMapping;
import org.jeecg.modules.golf.plan.query.PlanQueryService;
import org.jeecg.modules.golf.plan.service.IPlanService;
import org.jeecg.modules.golf.plan.service.IPlanTypeMappingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiguansheng
 * @date 2020/10/25
 **/
@Service
public class PlanQueryServiceImpl implements PlanQueryService {

    @Resource
    private IPlanService planService;

    @Resource
    private IPlanTypeMappingService planTypeMappingService;

    /**
     * 方案查询
     *
     * @param planQueryDTO
     * @return
     */
    @Override
    public IPage<PlanPage> page(PlanQueryDTO planQueryDTO) {
        List<Long> planIdList = null;
        if(planQueryDTO.getPlanTypeId() != null){
            PlanTypeMapping planTypeMapping = new PlanTypeMapping();
            planTypeMapping.setPlanTypeId(planQueryDTO.getPlanTypeId());
            QueryWrapper<PlanTypeMapping> planTypeMappingQueryWrapper = new QueryWrapper<>(planTypeMapping);
            List<PlanTypeMapping> planTypeMappingList =planTypeMappingService.list(planTypeMappingQueryWrapper);
            planIdList = planTypeMappingList.stream().map(PlanTypeMapping::getPlanId).collect(Collectors.toList());
            if(planIdList.isEmpty()){
               return new Page();
            }
        }

        QueryWrapper<Plan> planQueryWrapper  = new QueryWrapper<>();
        planQueryWrapper.eq(StringUtils.isNotEmpty(planQueryDTO.getTitle()),"title",planQueryDTO.getTitle());
        planQueryWrapper.in(CollectionUtil.isNotEmpty(planIdList),"id",planIdList);
        IPage<Plan> planPage = planService.page(planQueryDTO,planQueryWrapper);
        if(planPage.getTotal() ==0){
            return new Page<>();
        }
        IPage<PlanPage> planPageIPage = new Page<>(planPage.getCurrent(),planPage.getSize(),planPage.getTotal());
        List<Plan> planList = planPage.getRecords();
        Map<Long,List<PlanTypeMapping>> planTypeMap = queryPlanType(planList);
        List<PlanPage> planPageList = new ArrayList((int)planPage.getTotal());
        for(Plan plan  :planList){
            PlanPage item = new PlanPage();
            BeanUtil.copyProperties(plan,item);
            List<PlanTypeMapping> planTypeMappingList = planTypeMap.get(item.getId());
            if(planTypeMappingList != null){
                item.setPlanTypeJoin(planTypeMappingList.stream().map(PlanTypeMapping::getPlanTypeName).collect(Collectors.joining(",")));
            }
            planPageList.add(item);
        }
        planPageIPage.setRecords(planPageList);
        return planPageIPage;
    }

    /**
     * 查询方案的方案分类集合
     * @param planList
     * @return
     */
    private Map<Long,List<PlanTypeMapping>> queryPlanType(List<Plan> planList){
       List<Long> planIdList =   planList.stream().map(Plan::getId).collect(Collectors.toList());
       List<PlanTypeMapping> planTypeMappingList = planTypeMappingService.selectByPlanIdList(planIdList);
       if(CollectionUtil.isEmpty(planTypeMappingList)){
           return new HashMap<>();
       }
       return   planTypeMappingList.stream().collect(Collectors.groupingBy(PlanTypeMapping::getPlanId));
    }

}
